﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Reflection;

namespace RobotEscape
{
    public static class Sound
    {
        static Stream clickStream = load("click.mp3");
        static Stream corruptorStream = load("corruptor.mp3");
        static Stream laserStream = load("laser.mp3");
        static Stream dieStream = load("powerup1.mp3");

        public static void Play(Sounds sound)
        {
            Stream stream = null;
            switch (sound)
            {
                case Sounds.Click:
                    stream = clickStream;
                    break;
                case Sounds.Corruptor:
                    stream = corruptorStream;
                    break;
                case Sounds.Die:
                    stream = dieStream;
                    break;
                case Sounds.Laser:
                    stream = laserStream;
                    break;
            }

            if (stream != null)
            {
                MediaElement me = new MediaElement();
                me.SetSource(stream);
                me.AutoPlay = true;
                me.CurrentStateChanged += new RoutedEventHandler(me_CurrentStateChanged);
                Game.Instance.Canvas.Children.Add(me);
            }
        }

        private static Stream load(string filename)
        {
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("RobotEscape.Assets.sounds." + filename);
            return stream;
        }

        static void me_CurrentStateChanged(object sender, RoutedEventArgs e)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                if (sender is MediaElement)
                {
                    MediaElement me = sender as MediaElement;
                    if (me.CurrentState == MediaElementState.Paused)
                    {
                        Game.Instance.Canvas.Children.Remove(me);
                    }
                }
            });
        }
    }

    public enum Sounds
    {
        Click,
        Die,
        Corruptor,
        Laser
    }
}
